package edu.sjsu.cs.queryopt.dbms.main;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;

public class AccessTypeOne {
	static SelectHelper sh = new SelectHelper();
	static MatchCol mc = new MatchCol();
	static FilterFactor ff = new FilterFactor();
	static CalCard cc = new CalCard();
	static boolean tietea = false;
	static Hashtable<String, ArrayList<String>> htb = new Hashtable<String,ArrayList<String>>();
	static ArrayList<String> keyofhtb = new ArrayList<String>();
	static ArrayList<String> cob = new ArrayList<String>();
	
	public void AccessTypeOne(ArrayList<String> outputContainer , ArrayList<String> conditionpod , ArrayList<String> frontColpod ) throws FileNotFoundException{
//		System.out.println("/-------AccessTypeOne ----/");
		cob.addAll(conditionpod);
//		System.out.println("outputContainer " + outputContainer);
//		System.out.println("conditionpod " + conditionpod);
//		System.out.println("frontColpod " + frontColpod);
		Hashtable<String, ArrayList<String>> ht = new Hashtable<String,ArrayList<String>>();
		ArrayList<String> KeyList = new ArrayList<String>();
		
		ArrayList<String> IndexList = new ArrayList<String>();
		IndexList= sh.StringToArrayList(outputContainer);
    	ArrayList<String> ColCon = new ArrayList<String>();
    	ArrayList<Integer> remember = new ArrayList<Integer>();
    	for(int index = 0 ; index < IndexList.size() ; index++){
    		if(IndexList.get(index).startsWith("T")){
    			KeyList.add(IndexList.get(index));
    			remember.add(index);
    		}
    	}
    	remember.add(IndexList.size());
    	keyofhtb.addAll(KeyList);
    	for(int in = 0 ; in < KeyList.size() ; in++)
    	{
    		for(int out = remember.get(in) + 1 ; out < remember.get(in+1) ; out++)
    		{
    			
    			ColCon.add(IndexList.get(out));
    		}
    		ht.put(KeyList.get(in), ColCon);
    		ColCon = new ArrayList<String>();
    	}
    	htb.putAll(ht);
    	if(IsJoinPredicate(conditionpod)){
    		ArrayList<String> allindex = getTableList(conditionpod , conditionpod);
    		indexOrIndexScan(ht,KeyList,  allindex , conditionpod);
    		
    	}else{
    		/***
    		 * Might need to be indexOrIndexScan(ht,KeyList,  conditionpod , conditionpod);
    		 * */
    	ArrayList<String> allindex = getTableList(conditionpod , frontColpod);
//    	System.out.println("ht " + ht);
//    	System.out.println("allindex " + allindex);
    	// Checkig for index only
//    	System.out.println("/------------not join----/");
    	indexOrIndexScan(ht,KeyList,  allindex , conditionpod);
    	}
//    	FindMatchCol(ht, conditionpod);
    	// checking matching col
	}
	
	/**
	 * Checking there is join predicate or not
	 * */
	
	public boolean IsJoinPredicate(ArrayList<String> conditionpod){
//		System.out.println("IsJoinPredicate now " );
//		System.out.println("IsJoinPredicate conditionpod " + conditionpod);
		for(int index = 0 ; index < conditionpod.size() ; index++){
			
			if(conditionpod.get(index).trim().equals(">") ||
					conditionpod.get(index).trim().equals("<") ||
					conditionpod.get(index).trim().equals("=")){
//				System.out.println("I found connecter");
				if(conditionpod.get(index-1).trim().contains(".")&& conditionpod.get(index+1).trim().contains(".")){
//					System.out.println("IT IS JOIN");
					return true;
				}
				
			}
			
		}
		
//		System.out.println("IT IS NOT JOIN");
		return false;
		
	}
	/**
	 * string ver of is predicate
	 * */
	public boolean isJoinPredicateForString(String equation){
		ArrayList <String> returnAl = new ArrayList<String>();
		String[] tokens = equation.trim().split(" ");
    	returnAl.addAll(Arrays.asList(tokens));
    	
    	for(int index = 0 ; index < returnAl.size() ; index++){
    		
    		if(returnAl.get(index).equals(">") ||
    				returnAl.get(index).equals("=")||
    				returnAl.get(index).equals("<")){
    			if(returnAl.get(index-1).trim().startsWith("T") &&
    					returnAl.get(index+1).trim().startsWith("T")){
    				return true;
    			}
    		}
    		
    	}
    	return false;
	}
	public String GetLeftOrRight(String equation , int leftorright){
		ArrayList <String> returnAl = new ArrayList<String>();
		String[] tokens = equation.trim().split(" ");
    	returnAl.addAll(Arrays.asList(tokens));
    	String reString = "";
    	for(int index = 0 ; index < returnAl.size() ; index++){
    		
    		if(returnAl.get(index).equals(">") ||
    				returnAl.get(index).equals("=")||
    				returnAl.get(index).equals("<")){
    			if(leftorright ==1){
    				reString = getTableName(returnAl.get(index-1).trim());
    				return reString;
    			}
    			else{
    				reString = getTableName(returnAl.get(index+1).trim());
    				return reString;
    			}
    			}
    		}
    		 return reString;
    	}
    	
	
	
	
	
	
	
	
	public static ArrayList<String> getTableList(ArrayList<String> conditionpod , ArrayList<String> frontColpod ){
		
		
		ArrayList<String> reAl = new ArrayList<String>();
//		System.out.println("conditionpod " + conditionpod);
//		System.out.println("frontColpod " + frontColpod);
		for(int index = 0; index < conditionpod.size() ; index++){
			if(conditionpod.get(index).startsWith("T")){
			reAl.add(conditionpod.get(index));
			}
			
		}
		
		for(int index1 = 0; index1 < frontColpod.size() ; index1++){
		
			if(frontColpod.get(index1).charAt(0) == 'T' && !reAl.contains(frontColpod.get(index1))){
				
			reAl.add(frontColpod.get(index1));
			}
		}
//		System.out.println("reAl2 " + reAl);
		return reAl;
		
	}
	/*
	 * Index only can after where and after select
	 * */
	public int indexOrIndexScan( Hashtable<String, ArrayList<String>> ht,  ArrayList<String> keyset,  
				ArrayList<String> allindex , ArrayList<String>  conditionpod) throws FileNotFoundException{
		
		int counter = 0;
//		System.out.println("indexOrIndexScan " + allindex);
		ArrayList<String> newSet = new ArrayList<String>();
		Hashtable <String, Integer> htc = new Hashtable <String, Integer> ();
		for(int index = 0 ; index < ht.size() ; index++){
			ArrayList<String> htlist = getRidOfA(ht.get(keyset.get(index)));
//			System.out.println("htlist " + htlist);
			for(int in = 0 ; in < allindex.size() ; in ++){
				if(keyset.get(index).trim().startsWith(getTableName(allindex.get(in)).trim())){
//					System.out.println("keyset.get(index) " + keyset.get(index) + " contians  " +  allindex.get(in));
					for(int ii = 0 ; ii < allindex.size() ; ii++){
//						System.out.println("(allindex.get(ii).trim()).trim()) " + allindex.get(ii).trim().trim() + " ii " + ii);
//						System.out.println("getColNumer(allindex.get(ii).trim()).trim()) " + getColNumer(allindex.get(ii).trim()).trim());
//						System.out.println("getTableNameFromIndex(keyset.get(index).trim())" + getTableNameFromIndex(keyset.get(index).trim()));
//						System.out.println("(getColNumer(allindex.get(ii).trim()).trim())" + (getTableName(allindex.get(ii).trim()).trim()));
						if(IsIntheList(htlist ,getColNumer(allindex.get(ii).trim()).trim())
								&& getTableNameFromIndex(keyset.get(index).trim()).equals((getTableName(allindex.get(ii).trim()).trim()))
								){
//							System.out.println("keyset.get(index) " + keyset.get(index));
//							System.out.println("allindex.get(ii) " + allindex.get(ii));
//							System.out.println("FOUNRD!!!!!!!!!!!!!");
							counter ++;
						}
					}
//					System.out.println("counter " + counter);
					htc.put(keyset.get(index), counter);
					if(!newSet.contains(keyset.get(index))){
						newSet.add(keyset.get(index));
					
					}
					counter = 0;
				}
			}
			
		}
		
//		System.out.println("htC " + htc);
		/**
		 * 1: R SCAN
		 * 2: INDEX ONLY
		 * 3: I CAN BUT NOT INDEX ONLY
		 * */
//		System.out.println("allindex " + allindex);
		int answer = IsIndexOnly(htc , newSet , allindex );
		if(answer == 2){
			
			FindTheBestMatchingIndex(htc, newSet , conditionpod , ht);
		}
		else if(answer == 1){
//			System.out.println("R -S DON'T NEED TO FIND INDEX");
//			System.out.println("dsa dbms " + DBMS.PlanTable );
		}
		else{
			/**
			 * Something is here so we need to check it
			 * 1. from condidtion list need to handle two cases
			 * 	 		case1: read everything if there is not range predicate  "<" or ">"
			 * 				-> if there is range then do not read after that
			 * 			case2: read everything until end
			 * 2. From best match part get the matching part
			 * 			case: index is increasing seq
			 * 3. IF there is tie then choose smaller predicate index
			 * */
//			ArrayList<String> AfterWhereAfterGetRidOfScreening = mc.GetPotentialCol(conditionpod);
//			System.out.println("AfterWhereAfterGetRidOfScreening " + AfterWhereAfterGetRidOfScreening);
			Hashtable<String, ArrayList<String>> matchinght =  mc.getMatchingColInIndexs(ht , newSet);
//			System.out.println("matchinght " + matchinght);
			
			Hashtable<String, Integer> afterfindit =
					mc.findNewMatchingCol(conditionpod, matchinght, newSet);
//			System.out.println("/**********************/");
//			System.out.println("conditionpod " + conditionpod);
//			System.out.println("ht " + ht);
//			System.out.println("newSet " + newSet);
//			System.out.println("afterfindit " + afterfindit);
//			System.out.println("AfterWhereAfterGetRidOfScreening " + AfterWhereAfterGetRidOfScreening);
//			System.out.println("matchinght " + matchinght);
//			System.out.println("/**********************/");
			ArrayList<String> ne = new ArrayList<String>(); 		
			ne.addAll(afterfindit.keySet());
			
			if((afterfindit.size() == 0) && Collections.max(htc.values()) > 0 ){
				/**
				 * No matching case but i could have the other choose
				 * 
				 * */
//				System.out.println("This is NO MATCHING BUT THERE IS SOME INDEX");
				if(mc.isTie(afterfindit, newSet, htc)){
//					System.out.println("THIS IS TIE SITUATION");
				}
				else{
					
//					System.out.println("THIS IS TIE  NOT SITUATION");
					String onlyone = "";
					onlyone = mc.isNotTie(htc, newSet);
					DBMS.PlanTable.set(2, "0");
					DBMS.PlanTable.set(3, onlyone);
				}				
			}
			else{
//				System.out.println("THIS IS THE SITUATION THAT WE HAVE MATCHING COL IN FRONT it can be tie");
				//ht 
				if(mc.ISTIE(conditionpod, ht, ne)){
//					System.out.println("is it local " +ne);
					if(isLocal(conditionpod)){
					
					tietea = mc.ISTIE(conditionpod, ht, ne);
//					System.out.println("THIS IS TIE " + ne);
//					System.out.println("cc pl " + CalCard.prediTable);
					String minFF = BreakTie(conditionpod);
//					System.out.println("minFF " + minFF);
					String name = getAccessNameOnTie(minFF , ne , ht) ;
//					System.out.println("name name " + name);
					//bestmatching
					DBMS.PlanTable.set(2, mc.getTieVale(conditionpod, ht, newSet));
					DBMS.PlanTable.set(3, name);
					}
					else{
						/**
						 * no local case
						 * */
						ArrayList<String> names = nameOfTABLE(conditionpod);
						String tableone = cc.GetNumOfCalbyreadingline(names.get(0)+".tab");
						String tabletwo = cc.GetNumOfCalbyreadingline(names.get(1)+".tab");
//						System.out.println("no local tableone " + tableone);
//						System.out.println("no local tabletwo " + tabletwo);
						String acname =  " " ;
						DBMS.PlanTable.set(2, mc.getTieVale(conditionpod, ht, newSet));
						if(Integer.parseInt(tableone) > Integer.parseInt(tabletwo)){
							for(int a = 0 ; a < ne.size() ; a++){
								if(ne.get(a).trim().startsWith(names.get(0))){
									acname = ne.get(a);
								}
							}
							
						}
						else{
							for(int a = 0 ; a < ne.size() ; a++){
								if(ne.get(a).trim().startsWith(names.get(1))){
									acname = ne.get(a);
								}
							}
							
						}
//						System.out.println("acname " + acname);
						mc.bestmatching = acname;
						DBMS.PlanTable.set(3, acname);
//						System.out.println("cc pl " + CalCard.prediTable);
//						System.out.println("dbms " + DBMS.PlanTable);
					}
				
				}
				else{
//					System.out.println("THIS IS NOT TIE ");
					DBMS.PlanTable.set(3, mc.getNameForHostMatchCol(conditionpod, ht, ne));
					DBMS.PlanTable.set(2, mc.GetMatchColForNoTIe(conditionpod, ht, ne));
					
				}

			}
			
			
		
		}
		
		return 0;
		
	}
	
	public String getAccessNameOnTie(String FFvalue , ArrayList<String> ne,  Hashtable<String, ArrayList<String>> ht){
		String nameofIndex = "" ;
//		System.out.println("/&&&&&&&&&&&&&&&&&&&&&/");
//		System.out.println("FFvalue " + FFvalue);
//		System.out.println("ne " + ne);
//		System.out.println("ht " + ht);
//		System.out.println("CalCard.prediTable " + CalCard.prediTable);
		boolean join = false;
		int rem = 0;
		
		for(int index = 0 ; index < CalCard.prediTable.size() ; index++){
			
			if(CalCard.prediTable.get(index).contains(FFvalue)){
				 nameofIndex = CalCard.prediTable.get(index).get(6);
					if(isJoinPredicateForString(nameofIndex)){
							join =true;
							rem = index;
							
					}
					
			}
			
		}

		String[] tokens = nameofIndex.split(" ");
		String tablename = "";
		if(join == true){
			
			for(int ii = 0 ;ii < CalCard.prediTable.get(rem).size() ; ii++){
				if(CalCard.prediTable.get(rem).get(ii).trim().equals(FFvalue)){
					if(ii == 4){
						tablename = tokens[2];
					}
					else{
						tablename = tokens[0];
					}
				}
			}
			
		}
		else{
			tablename = tokens[0];
		}
	

	
		String reVal = "";
		for(int i = 0 ;  i < ne.size() ; i++){
			
			if(getTableNameFromIndex(ne.get(i)).trim().equals(getTableName(tablename))){
				 reVal = ne.get(i);
			}
			
		}
		

//		System.out.println("reVal " + reVal);
		return   reVal;
		
	}

	
	/**
	 * To Break the tie we see the 	FF value
	 * smaller will be first
	 * 
	 * */
	
	public String BreakTie(ArrayList<String> conditionpod){
		double compare = 0.0 ;
		ArrayList<Double> db = new ArrayList<Double>();
//		System.out.println("conditionpod conditionpod " + conditionpod );
		String joinpart = "";
		for(int index = 0 ; index < conditionpod.size() ; index++){
			if(conditionpod.get(index).trim().equals("=")){
				String name = conditionpod.get(index-1) + " " + "=" + conditionpod.get(index+1);
				if(isJoinPredicateForString(name)){
					joinpart = name;
				}
			}
			
		}
		
		
		for(int index = 0 ; index < CalCard.prediTable.size() ; index++){
			db.add(Double.parseDouble(CalCard.prediTable.get(index).get(3)));
		    
			if(CalCard.prediTable.get(index).contains(joinpart)){
//				System.out.println("CalCard.prediTable.get(index) " + CalCard.prediTable.get(index));
				db.add(Double.parseDouble(CalCard.prediTable.get(index).get(4)));
			}
			
		
		}
		double dbs = Collections.min(db);
		String minValff = Double.toString(dbs);
		
		return minValff;
	}
	
	/**
	 * Update 
	 * */
	
	
	/**
	 * Get the all index col
	 * [T1.2, =, F, AND, T1.3, =, Emily] - > [ 2 , 3]
	 * @return 
	 * */
	
	public static ArrayList<String> getAllIndexesFromOneRow(ArrayList<String> con){
		
		ArrayList<String> al = new ArrayList<String>();
		
		for(int index = 0 ; index < con.size() ; index++){
			
			if(con.get(index).startsWith("T")){
				al.add(getColNumer(con.get(index).trim()));
				
			}
			
		}
		
		return al;
	
	}
	
	
	
	
	/**
	 * Finding best matching index from the list
	 * 
	 * @param htc :htC {T1X3=1, T1X2=1, T1X1=2}
	 * */
	public static void FindTheBestMatchingIndex(Hashtable <String, Integer> htc , 
				ArrayList<String> newSet ,  ArrayList<String> conditionpod, Hashtable<String, ArrayList<String>> ht){
		
		int max = Collections.max(htc.values());
		
		if(isOnlyBestMatchingList(max , htc, newSet )){
			String some = FindOnlyCaseBest(max , htc , newSet);
		
			
			DBMS.PlanTable.set(2, Integer.toString(max));
			DBMS.PlanTable.set(3, FindOnlyCaseBest(max , htc , newSet));
			
		}
		else{
			/**
			 * More than one best matching indexes are here
			 * */
			ArrayList<String> tieIndexList = SearchForTieFF(max , htc , newSet);
			
		}
		
	}
	/**
	 * TOGET THE MATCHING COL FROM THERE AFTHER WHERE
	 * GET RID OF SCREENING PART 
	 * 
	 * */
	public static int DeleteScreenIndex(int max , String nameOfIndex , 
				Hashtable <String, Integer> htc, ArrayList<String> conditionpod,
				Hashtable<String, ArrayList<String>> ht){
//		System.out.println("max " + max);
//		System.out.println("nameOfIndex " + nameOfIndex);
//		System.out.println("htc " + htc);
//		System.out.println("conditionpod " + conditionpod);
//		System.out.println("ht " + ht);
		ArrayList<String> ScreenList =  ht.get(nameOfIndex);
//		System.out.println("ScreenList" + ScreenList);
		ArrayList<String> cond = new ArrayList<String>();
		ArrayList<String> icond = new ArrayList<String>();
		String tbname = sh.getTableNames(nameOfIndex);
		for(int i = 0 ; i <conditionpod.size() ; i++ ){
			if(conditionpod.get(i).trim().startsWith(tbname.trim()) ){
				cond.add(getColNumer(conditionpod.get(i).trim()));
			}
			
		}
		//getColNum
		for(int ii =  0 ; ii < ScreenList.size() ; ii++ )
		{
				icond.add(getColNum(ScreenList.get(ii)));
		}
		
//		System.out.println("cond " + cond);
//		System.out.println("icond " + icond);
		if(ScreenList.size() == max){
			
			return max;
			
		}
		int tracker = max;
		for(int index = 1 ; index <icond.size() ; index++)
		{
			if(IsIntheList(cond , icond.get(index).trim() )){
				tracker--;
			}
		}
		
		
//		System.out.println("tracker " + tracker);
		return tracker;
		
	}
	/**
	 * Function is looking for Matching col
	 * How ? CASE1: First col always matching
	 * 		 CASE2: From Second col to end: if col number is increasing sequencially then matching col
	 * 		For example ( 1a , 2a, 3a ) --> matching col 1,2,3
	 * 					 ( 4a , 3a , 2a) -> matching col 4
	 * 					(1a , 3a , 2a ) --> matching col 1
	 * 	  
	 * 
	 * 
	 * */

	
	/**
	 * Get the Tie indexs list
	 * 
	 * */
	public static ArrayList<String> SearchForTieFF(int max , Hashtable <String, Integer> htc , ArrayList<String> newSet){
		ArrayList<String> al = new ArrayList<String>();
		for(int index = 0; index < htc.size() ; index++){
			
			if(max == htc.get(newSet.get(index))){
				al.add(newSet.get(index));
			}
			
		}
		return al;
	}
	/**
	 * for using this function when max value is in the ONLY one Index
	 * 
	 * @return String name of best matching index
	 * */
	public static String FindOnlyCaseBest(int max , Hashtable <String, Integer> htc , ArrayList<String> newSet){
		
		for(int index = 0; index < htc.size() ; index++){
			
			if(max == htc.get(newSet.get(index))){
				return newSet.get(index);
			}
			
		}
		return "It can not be happen like I am being a super star";
	}
	/**
	 * Finding only best matching 
	 * 
	 * @return true : only best matching
	 * 		 false :  it is not only best matching FML
	 * 
	 * */
	public static boolean isOnlyBestMatchingList(int max , Hashtable <String, Integer> htc , ArrayList<String> newSet){
		
		int tracker = 0;
	
		for(int index = 0 ; index < htc.size() ; index++){
			if(max == htc.get(newSet.get(index)) ){

				tracker++;
			}
		}
		
		if(tracker == 1){
			return true;
		}
		else{
			return false;
		}
	}
	/**
	 * 1: R SCAN
	 * 2: INDEX ONLY
	 * 3: I CAN BUT NOT INDEX ONLY
	 * */
	
	public int IsIndexOnly(Hashtable<String, Integer> ht ,ArrayList<String> keyset , ArrayList<String> allindex){
		
		int bigger = 0;
		String name = ""; // store best matching
		for(int index = 0 ; index < keyset.size() ; index++){
//			System.out.println("ht.get(keyset.get(index))  " + ht.get(keyset.get(index)) );
			if(ht.get(keyset.get(index)) > bigger){
				bigger = ht.get(keyset.get(index));
				name = keyset.get(index);
			}
			
			
		}
//		System.out.println("where is I ? " + name);
//		System.out.println("where is I ht ? " + ht);
		String tbback  = "";
		String front = "";
		if(name.length() > 0){
			tbback = getColNum(htb.get(name).get(0));
			front = getTableNameFromIndex(name);
		}
		 
		String prediname = "";
//		System.out.println("where is I ? " +front.trim()+"."+ tbback.trim());
		prediname = front.trim()+"."+ tbback.trim();
		boolean sit = false;
		for(int index = 0 ; index < cob.size() ; index++){
			if(cob.get(index).trim().equals(prediname)){
				
				if(cob.get(index+1).trim().equals("IN")){
					
					sit = true;
				}
				
			}
		}
		if(bigger == 0){
//			System.out.println("R-SCAN");
			DBMS.PlanTable.add("R");
			DBMS.PlanTable.add(" ");
			DBMS.PlanTable.add(" ");
			DBMS.PlanTable.add(" ");
			DBMS.PlanTable.add("S");
			DBMS.PlanTable.add("N");
		
			return 1;
		}
		else if(bigger == allindex.size()){
//			System.out.println("INDEX ONLY ALL COL USING INDEX");
			DBMS.PlanTable.add("I");

			DBMS.PlanTable.add("MATCHCOLS: LATER ");
			DBMS.PlanTable.add("ACCESSNAME: LATER ");
			DBMS.PlanTable.add("Y");
			DBMS.PlanTable.add(" ");
			DBMS.PlanTable.add("N");
			return 2;
		}
		else{
//			System.out.println("NOT INDEX ONLY BUT SOME COL USING INDEX");			
			if(sit){
				DBMS.PlanTable.add("N");
			}
			else{
			DBMS.PlanTable.add("I");
			}	
			DBMS.PlanTable.add("MATCHCOLS: LATER ");
			DBMS.PlanTable.add("ACCESSNAME: LATER ");
			DBMS.PlanTable.add("N");
			DBMS.PlanTable.add(" ");
			DBMS.PlanTable.add("N");
			return 3;
		}
		
	}
	/**
	 * For finding index from 
	 * htlist is the target list that want to compare
	 * number: index number in the after select or where
	 * 
	 * it is basically doing contains functions 
	 * 
	 * @param ArrayList<String>
	 * @param String
	 * 
	 * @return  true: if number is in the list
	 * 			false: if number is not in the list
	 * 
	 * */
	public static boolean IsIntheList(ArrayList<String> htlist  , String number)
	{
		
		for(int index = 0  ; index < htlist.size() ; index++){
			if(htlist.get(index).trim().equals(number.trim())){
				return true;
			}
			
		}
		return false;
		
	}
	/**
	 * 1A , 3A  -> 1, 3
	 * 
	 * */
	public ArrayList<String> getRidOfA(ArrayList<String> al){
		 ArrayList<String> re = new  ArrayList<String>();
		for(int index = 0 ; index < al.size() ; index++){
			re.add(getColNum(al.get(index).trim()));
		}
		
		return re;
		
	}
	/**
	 * GET TABLE NAME FROM INDEX - > T1X2 - > T1
	 * */
	
	 public String getTableNameFromIndex(String indexname){
		 String re = "";
		 int index = 0;
		 while(indexname.charAt(index) != 'X'){
			 re += indexname.charAt(index);
			 index++;
		 }
//		 System.out.println("getTableNameFromIndex " + re);
		 return re;
		 
	 }
	/**
	 * 
	 * T1.6 -> T1
	 * */
	
	public String getTableName(String tableWithCol){
		
		String re = "";
		int index = 0;
		while(tableWithCol.charAt(index) != '.'){
			
			re += tableWithCol.charAt(index);
			index++;
		}
		return re;
		
	}
	/**
	 * 
	 * T1.6 -> 6
	 * */
	public static String getColNumer(String tableWithCol){
		
		String re = "";
		int index = 0 ;
		
		while(tableWithCol.charAt(index) != '.'){
			
			
			index++;
		}
		index++;
		re = tableWithCol.substring(index).trim();
		
		
		
		return re;
		
		
	}
	/**
	 * 1A -> 1
	 * */
	
    public static String getColNum (String colnumber)
    {
    	String number = " ";
    	int index = 0;
    	while(colnumber.charAt(index) != 'D' &&
    			colnumber.charAt(index) != 'A'
    			)
    	{
    		number += colnumber.charAt(index);
    		index++;
    	
    	}
    	return number;
    	
    }
    /**
     * T1 -> 1
     * */
    public static String tableNumberFromTable(String table){
    	int tracker= 0;
    	for(int index = 0 ; index < table.length() ; index++){
    		
    		if(table.trim().charAt(index) == 'T'){
    			break;
    		}
    		tracker++;
    	}
    	
    	return table.substring(tracker+1);
    }
    public boolean isLocal(ArrayList<String>  conditionpod){
    	
    	for(int i = 0 ; i < conditionpod.size() ; i++){
    		
    		if(conditionpod.get(i).equals(">") ||  conditionpod.get(i).equals("<") || conditionpod.get(i).equals("=")){
    			if(!conditionpod.get(i+1).startsWith("T")){
    				return true;
    			}
    		}
    	}
    	
    	return false;
    	
    }
    public ArrayList<String> nameOfTABLE(ArrayList<String>  conditionpod){
    	
    	ArrayList<String> al = new ArrayList<String>();
    	for(int i = 0 ; i < conditionpod.size() ; i++){
    	
    			if(conditionpod.get(i).trim().startsWith("T")){
    				al.add(getTableName(conditionpod.get(i)));
    				
    			}
    	}
    	return al;
    }

}
